﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YABE.Models;

namespace YABE.Controllers
{
	[HandleError]
	public class AdminController : Controller
	{
		[Authorize]
		public ActionResult Index()
		{
			return View("Index");
		}

	    private Models.IPostDL pd;

        public AdminController()
        {
            this.pd = new YABE.Models.PostDL();
        }

        public AdminController(IPostDL postDL)
        {
            this.pd = postDL;
        }

		[Authorize]
		public ActionResult About()
		{
			return View("About");
		}
		
		[Authorize]
		public ActionResult List()
		{
			var monthList = from month in pd.GetPostMonths()
							select new
								{
									ID = month.ToString("yyyy/MM"),
									Name = month.ToString("MMMM yyyy")
								};

			ViewData["monthList"] = new SelectList(monthList, "ID", "Name");
			return View("List");
		}
		
		[AcceptVerbs("GET")]
		public ActionResult Login(string returnUrl)
		{
			if (returnUrl == null)
				ViewData["ReturnUrl"] =  "/Posts/Index";
			else
				ViewData["ReturnUrl"] = returnUrl;
			return View("Login");
		}

		[ActionName("Login"), AcceptVerbs("POST")]
		public ActionResult ValidateLogin(string returnUrl, string userid, string password)
		{
			ViewData["ReturnUrl"] = returnUrl;

			if (System.Web.Security.Membership.ValidateUser(userid, password))
			{
				System.Web.Security.FormsAuthentication.SetAuthCookie(userid, false);
				
				if (returnUrl != null)
					return Redirect(returnUrl);
			}
			else
			{
				ViewData["Failure"] = true;
				return View("Login",ViewData);
			}
			
			return RedirectToAction("Index", "Posts");
		}
		[Authorize]
		public ActionResult Logout()
		{
			System.Web.Security.FormsAuthentication.SignOut();
			return RedirectToAction("Index","Posts");
		}
		[Authorize]
		public void Profile()
		{
			RedirectToAction("Profile");
		}

		public ActionResult Test()
		{
			return View();
		}

		[Authorize]
		public ActionResult Scrub()
		{
			pd.ScrubPosts();

			return new EmptyResult();
		}
	}
}
